<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="收款确认单唯一值" prop="skqrId">
        <el-input
          v-model="queryParams.skqrId"
          placeholder="请输入收款确认单唯一值"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收款单号" prop="skCode">
        <el-input
          v-model="queryParams.skCode"
          placeholder="请输入收款单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收款日期" prop="skDate">
        <el-date-picker clearable
          v-model="queryParams.skDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择收款日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="收款类型" prop="skLx">
        <el-input
          v-model="queryParams.skLx"
          placeholder="请输入收款类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="现金类型" prop="skXjLx">
        <el-input
          v-model="queryParams.skXjLx"
          placeholder="请输入现金类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收款原因" prop="skYy">
        <el-input
          v-model="queryParams.skYy"
          placeholder="请输入收款原因"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="零售单号" prop="skRetailCode">
        <el-input
          v-model="queryParams.skRetailCode"
          placeholder="请输入零售单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收款来源" prop="skLy">
        <el-input
          v-model="queryParams.skLy"
          placeholder="请输入收款来源"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收款所属云仓" prop="skYc">
        <el-input
          v-model="queryParams.skYc"
          placeholder="请输入收款所属云仓"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="云仓类型" prop="skYcLx">
        <el-input
          v-model="queryParams.skYcLx"
          placeholder="请输入云仓类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收款金额" prop="skJe">
        <el-input
          v-model="queryParams.skJe"
          placeholder="请输入收款金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收款金额大写" prop="skJeUp">
        <el-input
          v-model="queryParams.skJeUp"
          placeholder="请输入收款金额大写"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="应付抵扣" prop="skYfdk">
        <el-input
          v-model="queryParams.skYfdk"
          placeholder="请输入应付抵扣"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户编码" prop="skKhCode">
        <el-input
          v-model="queryParams.skKhCode"
          placeholder="请输入客户编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户名称" prop="skKh">
        <el-input
          v-model="queryParams.skKh"
          placeholder="请输入客户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="金蝶客户代码" prop="skJdKhCode">
        <el-input
          v-model="queryParams.skJdKhCode"
          placeholder="请输入金蝶客户代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="金蝶客户名称" prop="skJdKh">
        <el-input
          v-model="queryParams.skJdKh"
          placeholder="请输入金蝶客户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单据编号" prop="skDocumentCode">
        <el-input
          v-model="queryParams.skDocumentCode"
          placeholder="请输入单据编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开单公司" prop="skKdCompany">
        <el-input
          v-model="queryParams.skKdCompany"
          placeholder="请输入开单公司"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开单时间" prop="skKdTime">
        <el-date-picker clearable
          v-model="queryParams.skKdTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择开单时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="税率点" prop="taxPoint">
        <el-input
          v-model="queryParams.taxPoint"
          placeholder="请输入税率点"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开单金额" prop="skKdJe">
        <el-input
          v-model="queryParams.skKdJe"
          placeholder="请输入开单金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="税额" prop="taxPointJe">
        <el-input
          v-model="queryParams.taxPointJe"
          placeholder="请输入税额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发票类型" prop="invoiceType">
        <el-select v-model="queryParams.invoiceType" placeholder="请选择发票类型" clearable>
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="合同编号" prop="skHtCode">
        <el-input
          v-model="queryParams.skHtCode"
          placeholder="请输入合同编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="确认收款" prop="skQrsk">
        <el-input
          v-model="queryParams.skQrsk"
          placeholder="请输入确认收款"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否同步" prop="skSftb">
        <el-input
          v-model="queryParams.skSftb"
          placeholder="请输入是否同步"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="确认人" prop="skQrr">
        <el-input
          v-model="queryParams.skQrr"
          placeholder="请输入确认人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="确认时间" prop="skSj">
        <el-date-picker clearable
          v-model="queryParams.skSj"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择确认时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['fina:confirm:order:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['fina:confirm:order:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['fina:confirm:order:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="el-icon-refresh"
            size="mini"
            @click="handleSync"
            v-hasPermi="['fina:confirm:order:sync']"
        >数据同步</el-button>
        </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['fina:confirm:order:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="serialNum" />
      <el-table-column label="收款确认单唯一值" align="center" prop="skqrId" />
      <el-table-column label="收款单号" align="center" prop="skCode" />
      <el-table-column label="收款日期" align="center" prop="skDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.skDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="收款类型" align="center" prop="skLx" />
      <el-table-column label="现金类型" align="center" prop="skXjLx" />
      <el-table-column label="收款原因" align="center" prop="skYy" />
      <el-table-column label="零售单号" align="center" prop="skRetailCode" />
      <el-table-column label="收款来源" align="center" prop="skLy" />
      <el-table-column label="收款所属云仓" align="center" prop="skYc" />
      <el-table-column label="云仓类型" align="center" prop="skYcLx" />
      <el-table-column label="收款金额" align="center" prop="skJe" />
      <el-table-column label="收款金额大写" align="center" prop="skJeUp" />
      <el-table-column label="备注" align="center" prop="skRemark" />
      <el-table-column label="应付抵扣" align="center" prop="skYfdk" />
      <el-table-column label="客户编码" align="center" prop="skKhCode" />
      <el-table-column label="客户名称" align="center" prop="skKh" />
      <el-table-column label="金蝶客户代码" align="center" prop="skJdKhCode" />
      <el-table-column label="金蝶客户名称" align="center" prop="skJdKh" />
      <el-table-column label="单据编号" align="center" prop="skDocumentCode" />
      <el-table-column label="开单公司" align="center" prop="skKdCompany" />
      <el-table-column label="开单时间" align="center" prop="skKdTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.skKdTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="税率点" align="center" prop="taxPoint" />
      <el-table-column label="开单金额" align="center" prop="skKdJe" />
      <el-table-column label="税额" align="center" prop="taxPointJe" />
      <el-table-column label="发票类型" align="center" prop="invoiceType" />
      <el-table-column label="合同编号" align="center" prop="skHtCode" />
      <el-table-column label="确认收款" align="center" prop="skQrsk" />
      <el-table-column label="是否同步" align="center" prop="skSftb" />
      <el-table-column label="确认人" align="center" prop="skQrr" />
      <el-table-column label="确认时间" align="center" prop="skSj" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.skSj, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['fina:confirm:order:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['fina:confirm:order:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改收款确认单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="收款确认单唯一值" prop="skqrId">
          <el-input v-model="form.skqrId" placeholder="请输入收款确认单唯一值" />
        </el-form-item>
        <el-form-item label="收款单号" prop="skCode">
          <el-input v-model="form.skCode" placeholder="请输入收款单号" />
        </el-form-item>
        <el-form-item label="收款日期" prop="skDate">
          <el-date-picker clearable
            v-model="form.skDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择收款日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="收款类型" prop="skLx">
          <el-input v-model="form.skLx" placeholder="请输入收款类型" />
        </el-form-item>
        <el-form-item label="现金类型" prop="skXjLx">
          <el-input v-model="form.skXjLx" placeholder="请输入现金类型" />
        </el-form-item>
        <el-form-item label="收款原因" prop="skYy">
          <el-input v-model="form.skYy" placeholder="请输入收款原因" />
        </el-form-item>
        <el-form-item label="零售单号" prop="skRetailCode">
          <el-input v-model="form.skRetailCode" placeholder="请输入零售单号" />
        </el-form-item>
        <el-form-item label="收款来源" prop="skLy">
          <el-input v-model="form.skLy" placeholder="请输入收款来源" />
        </el-form-item>
        <el-form-item label="收款所属云仓" prop="skYc">
          <el-input v-model="form.skYc" placeholder="请输入收款所属云仓" />
        </el-form-item>
        <el-form-item label="云仓类型" prop="skYcLx">
          <el-input v-model="form.skYcLx" placeholder="请输入云仓类型" />
        </el-form-item>
        <el-form-item label="收款金额" prop="skJe">
          <el-input v-model="form.skJe" placeholder="请输入收款金额" />
        </el-form-item>
        <el-form-item label="收款金额大写" prop="skJeUp">
          <el-input v-model="form.skJeUp" placeholder="请输入收款金额大写" />
        </el-form-item>
        <el-form-item label="备注" prop="skRemark">
          <el-input v-model="form.skRemark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="应付抵扣" prop="skYfdk">
          <el-input v-model="form.skYfdk" placeholder="请输入应付抵扣" />
        </el-form-item>
        <el-form-item label="客户编码" prop="skKhCode">
          <el-input v-model="form.skKhCode" placeholder="请输入客户编码" />
        </el-form-item>
        <el-form-item label="客户名称" prop="skKh">
          <el-input v-model="form.skKh" placeholder="请输入客户名称" />
        </el-form-item>
        <el-form-item label="金蝶客户代码" prop="skJdKhCode">
          <el-input v-model="form.skJdKhCode" placeholder="请输入金蝶客户代码" />
        </el-form-item>
        <el-form-item label="金蝶客户名称" prop="skJdKh">
          <el-input v-model="form.skJdKh" placeholder="请输入金蝶客户名称" />
        </el-form-item>
        <el-form-item label="单据编号" prop="skDocumentCode">
          <el-input v-model="form.skDocumentCode" placeholder="请输入单据编号" />
        </el-form-item>
        <el-form-item label="开单公司" prop="skKdCompany">
          <el-input v-model="form.skKdCompany" placeholder="请输入开单公司" />
        </el-form-item>
        <el-form-item label="开单时间" prop="skKdTime">
          <el-date-picker clearable
            v-model="form.skKdTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择开单时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="税率点" prop="taxPoint">
          <el-input v-model="form.taxPoint" placeholder="请输入税率点" />
        </el-form-item>
        <el-form-item label="开单金额" prop="skKdJe">
          <el-input v-model="form.skKdJe" placeholder="请输入开单金额" />
        </el-form-item>
        <el-form-item label="税额" prop="taxPointJe">
          <el-input v-model="form.taxPointJe" placeholder="请输入税额" />
        </el-form-item>
        <el-form-item label="发票类型" prop="invoiceType">
          <el-select v-model="form.invoiceType" placeholder="请选择发票类型">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="合同编号" prop="skHtCode">
          <el-input v-model="form.skHtCode" placeholder="请输入合同编号" />
        </el-form-item>
        <el-form-item label="确认收款" prop="skQrsk">
          <el-input v-model="form.skQrsk" placeholder="请输入确认收款" />
        </el-form-item>
        <el-form-item label="是否同步" prop="skSftb">
          <el-input v-model="form.skSftb" placeholder="请输入是否同步" />
        </el-form-item>
        <el-form-item label="确认人" prop="skQrr">
          <el-input v-model="form.skQrr" placeholder="请输入确认人" />
        </el-form-item>
        <el-form-item label="确认时间" prop="skSj">
          <el-date-picker clearable
            v-model="form.skSj"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择确认时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="删除标志" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOrder, getOrder, delOrder, addOrder, updateOrder ,confirmSync} from "@/api/fina/confirmorder";

export default {
  name: "confirmOrder",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 收款确认单表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        skqrId: null,        skCode: null,        skDate: null,        skLx: null,        skXjLx: null,        skYy: null,        skRetailCode: null,        skLy: null,        skYc: null,        skYcLx: null,        skJe: null,        skJeUp: null,        skRemark: null,        skYfdk: null,        skKhCode: null,        skKh: null,        skJdKhCode: null,        skJdKh: null,        skDocumentCode: null,        skKdCompany: null,        skKdTime: null,        taxPoint: null,        skKdJe: null,        taxPointJe: null,        invoiceType: null,        skHtCode: null,        skQrsk: null,        skSftb: null,        skQrr: null,        skSj: null,      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询收款确认单列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        serialNum: null,        skqrId: null,        skCode: null,        skDate: null,        skLx: null,        skXjLx: null,        skYy: null,        skRetailCode: null,        skLy: null,        skYc: null,        skYcLx: null,        skJe: null,        skJeUp: null,        skRemark: null,        skYfdk: null,        skKhCode: null,        skKh: null,        skJdKhCode: null,        skJdKh: null,        skDocumentCode: null,        skKdCompany: null,        skKdTime: null,        taxPoint: null,        skKdJe: null,        taxPointJe: null,        invoiceType: null,        skHtCode: null,        skQrsk: null,        skSftb: null,        skQrr: null,        skSj: null,        delFlag: null,        createBy: null,        createTime: null,        updateBy: null,        updateTime: null      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.serialNum)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加收款确认单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const serialNum = row.serialNum || this.ids
      getOrder(serialNum).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改收款确认单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.serialNum != null) {
            updateOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    handleSync() {
      if (this.loading) {
        this.$modal.message("正常进行中，请不要重复点击...");
        return;
      }
      this.loading = true;
      this.$modal.confirm('是否确认同步数据？').then(() => {
        confirmSync().then(response => {
          if (response.code === 200) {
            this.$modal.msgSuccess("同步成功");
            this.getList();
          } else {
            this.$modal.msgError("同步失败");
          }
        }).catch(() => {
          this.$modal.msgError("同步失败");
        }).finally(() => {
          this.loading = false;
        });
      }).catch(() => {
        this.loading = false;
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const serialNums = row.serialNum || this.ids;
      this.$modal.confirm('是否确认删除收款确认单编号为"' + serialNums + '"的数据项？').then(function() {
        return delOrder(serialNums);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('fina/confirm/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
